package com.fsc.comadapoc1.presentation.managers
{
	

	
	import com.fsc.comadapoc1.domain.PortfolioModel;
	import com.fsc.comadapoc1.events.PanelChangeEvent;
	import com.fsc.comadapoc1.events.SelectionChangeEvent;
	
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.controls.Alert;

	

	
	public class PanelManagerPM extends EventDispatcher
	{
		/** The porfolios panel view. **/
		public const LOGIN_VIEW_STATE : String = "loginViewState";
		
		/** The porfolios panel view. **/
		public const PORTFOLIOS_VIEW_STATE : String = "portfoliosViewState";
		
		/** The loaded portfolio view state. **/
		public const PORTFOLIO_VIEW_STATE : String = "portfolioViewState";

		/** The subscriptions view state. **/
		public const SUBSCRIPTIONS_VIEW_STATE : String = "subscriptionsViewState";
		
		/** The subscribe view state. **/
		public const SUBSCRIBE_VIEW_STATE : String = "subscribeViewState";
		
		private var _currentState : String = LOGIN_VIEW_STATE;	
		
		
		/**
		 * Pending state used on result of animation timer
		 */ 
		private var pendingState:String;
		
		/**
		 * Contains objects to manager the currently selected portfolio
		 */ 
	
		[Inject]
		public var model:PortfolioModel;
		
		
		/**
		 * Delays switching of panels
		 */ 
		private var animationTimer:Timer;
		
		/**
		 * Manages Panel state change events 
		 **/
		
		[MessageHandler]
		public function handleStateChange(event:PanelChangeEvent):void{
			switch(event.state){
				case PanelChangeEvent.PORTFOLIO_VIEW_STATE:
					currentState = PORTFOLIO_VIEW_STATE;
					//setAnimationTimer(); ajwt dont need this unless we go for swipes
					break;
				case PanelChangeEvent.PORTFOLIOS_VIEW_STATE:
					currentState = PORTFOLIOS_VIEW_STATE;
					//setAnimationTimer(); ajwt dont need this unless we go for swipes
					break;
				case PanelChangeEvent.SUBSCRIPTIONS_VIEW_STATE:
					currentState = SUBSCRIPTIONS_VIEW_STATE;
					//setAnimationTimer(); ajwt dont need this unless we go for swipes
					break;		
				case PanelChangeEvent.SUBSCRIBE_VIEW_STATE:
					currentState = SUBSCRIBE_VIEW_STATE;
					//setAnimationTimer(); ajwt dont need this unless we go for swipes
					break;		
			}
		}
		
		private function setAnimationTimer():void{
			animationTimer = new Timer(600,1);
			animationTimer.addEventListener(TimerEvent.TIMER_COMPLETE, setPendingState);
			animationTimer.start();
		}
		
		/**
		 * Sets the pending state of the pending panel
		 */ 
		private function setPendingState(event:TimerEvent):void{
			animationTimer.stop();
			animationTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, setPendingState);
			animationTimer = null;
			
			currentState = pendingState;
		}
		
		
		/**
		 * Called when a new employee has been loaded.
		 **/
		[MessageHandler(selector="portfolioChange")] 
		public function onPortfolioChange(event:SelectionChangeEvent):void{
			
			//currentState = PORTFOLIO_VIEW_STATE;
			//if right panel state is direct reports update view to reflect new employees reports
			/*if(rightPanelPM.currentState == rightPanelPM.DIRECT_REPORTS_STATE){
				if(model.selectedEmployee.directReports){
					directReportsPanelPM.currentState = directReportsPanelPM.VIEW_REPORTS_STATE;
				}else{
					directReportsPanelPM.currentState = directReportsPanelPM.NO_REPORTS;
				}
			}	*/		
		}
		
		/**
		 * Get the current state of the presentation model.
		 */
		[Bindable]
		public function get currentState() : String
		{
			return _currentState;
		}
		
		/**
		 * Sets the current state of the presentation model.
		 */
		public function set currentState( state:String ) : void
		{
			_currentState = state;	
		}
	}
}